#include <iostream>
#include "src/Monochrome.hpp"

UIImage* background1;
UIImage* background2;
std::unique_ptr<UIScrollPanel> scrollpnl;

void OnLeftClick(UIElement* sender)
{
	if (sender->SourceWindow->GetBackgroundImage() == background1)
		sender->SourceWindow->SetBackgroundImage(background2);
	else
		sender->SourceWindow->SetBackgroundImage(background1);
}

void fileSave(UIWindow* window, int itemID)
{
	MessageBoxA(0, "Saved", "File", 0);
}

void fileClose(UIWindow* window, int itemID)
{
	MessageBoxA(0, "Closed", "File", 0);
}

void Textbox_OnKeyEvent(UIElement* sender, KeyEvt evt)
{
	if (evt.event_type & MC_KEY_PRESSED && evt.code == MC_RETURN)
	{
		MessageBoxA(0, ToUITextbox(sender)->Text.c_str(), "Textbox", 0);
	}
}

void Checkbox_OnCheck(UIElement* sender)
{
	if (static_cast<UICheckbox*>(sender)->Checked)
		MessageBoxA(0, "Checked", "Checkbox", 0);
	else
		MessageBoxA(0, "Unchecked", "Checkbox", 0);
}

int menu_bar_enabled = 1;

int main()
{
	UIWindow* window = new UIWindow();
	window->mcCreateModernWindow(1400, 800, "Monochrome");
	window->GetModernWindowCloseButton()->color = Color::Black;
	window->GetModernWindowCloseButton()->TextColor = Color::White;
	window->GetModernWindowCloseButton()->HoverOnColor = Color::DarkGray;
	window->Show();

	std::unique_ptr<UIMenuBar> menuBar(new UIMenuBar());
	std::unique_ptr<UIMenu> fileMenu(new UIMenu("File"));
	
	std::unique_ptr<UIMenuItem> saveMenuItem(new UIMenuItem("Save", 1));
	window->AddMenuItemCallback(1, fileSave);
	std::unique_ptr<UIMenuItem> closeMenuItem(new UIMenuItem("Close", 2));
	window->AddMenuItemCallback(2, fileClose);

	fileMenu->AddMenuItem(saveMenuItem.get());
	fileMenu->AddMenuItem(closeMenuItem.get());

	menuBar->AddMenu(fileMenu.get());
	window->SetMenuBar(menuBar.get());

	background1 = new UIImage(window, "C:\\Users\\FlareFlax\\Pictures\\trooper.jpg");
	background2 = new UIImage(window, "C:\\Users\\FlareFlax\\Pictures\\nightCity.jpg");
	window->SetBackgroundImage(background1);

	UIView* mainView = new UIView();

	std::unique_ptr<UIButton> btn(new UIButton(600, 600, 220, 50, "Change Background"));
	btn->AddLeftMouseClickEvent(OnLeftClick);
	btn->AddRightMouseClickEvent([&menuBar](UIElement* sender) {
		if (menu_bar_enabled == 1)
		{
			sender->SourceWindow->SetMenuBar(nullptr);
			menu_bar_enabled = 0;
		}
		else
		{
			sender->SourceWindow->SetMenuBar(menuBar.get());
			menu_bar_enabled = 1;
		}
	});
	mainView->Add(btn.get());

	std::unique_ptr<UITextbox> textbox(new UITextbox(100, 200, 240, 34, "Textbox 1", "Verdana", 16));
	textbox->AddKeyEvent(Textbox_OnKeyEvent);
	mainView->Add(textbox.get());

	std::unique_ptr<UITextbox> textbox2(new UITextbox(100, 240, 240, 34, "Textbox 2", "Verdana", 16));
	textbox2->AddKeyEvent([&textbox, &textbox2] (UIElement* sender, KeyEvt evt) {
		if (evt.code == MC_ENTER)
			textbox->Text = textbox2->Text;
	});
	mainView->Add(textbox2.get());

	scrollpnl = std::unique_ptr<UIScrollPanel>(new UIScrollPanel(540, 60));
	scrollpnl->SlotHeight = 36;
	scrollpnl->SlotWidth -= 8;
	scrollpnl->ScrollbarWidth += 8;

	for (int i = 0; i < 20; i++)
	{
		UIButton* btn = new UIButton(0, 0, 0, 0, "Item " + std::to_string(i + 1));
		btn->AddLeftMouseClickEvent([](UIElement* sender) {
			MessageBoxA(0, static_cast<UIButton*>(sender)->Text.c_str(), "UIScrollPanel Click Event", 0);
		});
		scrollpnl->AddElement(btn);
	}

	mainView->Add(scrollpnl.get());

	std::unique_ptr<UICombobox> combobox(new UICombobox(870, 60, 270, 28));

	combobox->AddItem("Red");
	combobox->AddItem("Green");
	combobox->AddItem("Blue");
	combobox->AddItem("Purple");
	combobox->AddItem("Cyan");
	combobox->AddItem("Magenta");

	combobox->AddSelectedIndexChangedEvent([&combobox](UIElement* sender, int index) {
		MessageBoxA(0, std::string("Item: " + combobox->GetItem(combobox->GetSelectedIndex())).c_str(), "Selected Index Changed", 0);
	});

	mainView->Add(combobox.get());

	std::unique_ptr<UIProgressBar> progressBar(new UIProgressBar(260, 500, 260, 16));
	progressBar->Value = 0;
	progressBar->Interval = 0.1f;
	mainView->Add(progressBar.get());

	std::unique_ptr<UICircularProgressBar> circularProgressBar(new UICircularProgressBar(260, 400, 36, 6));
	circularProgressBar->Value = 0;
	circularProgressBar->Interval = 0.1f;
	circularProgressBar->ProgressColor = Color::Green;
	mainView->Add(circularProgressBar.get());

	std::unique_ptr<UISlider> slider(new UISlider());
	slider->Position.y = 600;
	mainView->Add(slider.get());

	std::unique_ptr<UICheckbox> checkbox(new UICheckbox(100, 500, 24, "Checkbox", "Verdana", 14, 14.0f));
	checkbox->TextColor = Color::White;
	checkbox->AddCheckEvent(Checkbox_OnCheck);
	mainView->Add(checkbox.get());

	window->SetView(mainView);

	while (window->IsOpened())
	{
		progressBar->Value += 0.05f;
		circularProgressBar->Value += 0.05f;
		window->Update();
	}

	delete background1; // have to be deleted before the graphics in the window
	delete background2; // have to be deleted before the graphics in the window
	
	delete mainView;
	delete window;

	return 0;
}
